<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	public function index()
	{
		$data = array();
		$this->output->enable_profiler(TRUE);
		/*$user = $this->authenticate_user();
		if(!$user) {
			
		} else {
			$data['user'] = $user;
		}*/
		$this->load->view('home', $data);
	}
	
	
	private function authenticate_user()
	{
		$query = $this->db->get('users','1');
		if($query->num_rows())
		{
			$row = $query->result();
			$user= $row[0];
			$this->session->set_userdata('loggedin',TRUE);
			return $user;
		}
		$cookie= true;
		//$cookie = $this->get_facebook_cookie('123773181307','69f883df40665ca86f54e1a34eb12b14');
		if($cookie) $this->session->set_userdata('loggedin',TRUE);
		//$user = json_decode(file_get_contents( 'https://graph.facebook.com/me?access_token=' . $cookie['access_token']));
		$user->id  = 'asdasasdsdf';
		$query = $this->db->get_where('users',array('fbid'=>$user->id));
		if(!$query->num_rows())
		{
			/*$user_data = array(
				'id'=>uniqid('10_',true),
				'fbid'=>$user->id,
				'first_name'=>$user->first_name
				);
			$this->db->insert('users',$user_data);
			$user->id = $user_data['id'];*/
		}
		else
		{
			$row = $query->result();
			$user->id = $row[0]->id;
		
		}
		return $user;
	}
	private function get_facebook_cookie($app_id, $app_secret) {
	  $args = array();
	  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
	  ksort($args);
	  $payload = '';
	  foreach ($args as $key => $value) {
	    if ($key != 'sig') {
	      $payload .= $key . '=' . $value;
	    }
	  }
	  if (md5($payload . $app_secret) != $args['sig']) {
	    return null;
	  }
	  return $args;
	}
}
